<?php

namespace Admin\Models\res;

use Admin\Models\BaseModel;

class XhresQuestionbank extends BaseModel
{

    /**
     *
     * @var integer
     */
    public $iQBankId;

    /**
     *
     * @var string
     */
    public $sQBVersion;

    /**
     *
     * @var string
     */
    public $sQBDownUrl;

    /**
     *
     * @var string
     */
    public $sQBMd5;

    /**
     *
     * @var string
     */
    public $sQBUpdateVersion;

    /**
     *
     * @var string
     */
    public $sQBUpdateUrl;

    /**
     *
     * @var string
     */
    public $sQBUpdateMd5;

    /**
     *
     * @var string
     */
    public $sBookName;

    /**
     *
     * @var string
     */
    public $sCoverUrl;

    /**
     *
     * @var string
     */
    public $sGradeName;

    /**
     *
     * @var string
     */
    public $sCourseName;

    /**
     *
     * @var string
     */
    public $sSortName;

    /**
     *
     * @var integer
     */
    public $bAudio;

    /**
     *
     * @var integer
     */
    public $bDict;

    /**
     *
     * @var string
     */
    public $mBookIntro;

    /**
     *
     * @var string
     */
    public $mExtractRule;

    /**
     *
     * @var integer
     */
    public $iQBookSize;

    /**
     *
     * @var string
     */
    public $sPackInfo;

    /**
     *
     * @var string
     */
    public $tPackDate;

    /**
     *
     * @var string
     */
    public $mQBVersionDate;

    /**
     *
     * @var integer
     */
    public $iSchoolNum;

    /**
     *
     * @var integer
     */
    public $iPersonNum;

    /**
     *
     * @var integer
     */
    public $bDelete;

    /**
     *
     * @var string
     */
    public $tCreateDate;

    /**
     *
     * @var string
     */
    public $iCreateUserId;

    /**
     *
     * @var string
     */
    public $tLastModifyDate;

    /**
     *
     * @var string
     */
    public $iLastModifyUserId;

    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        parent::initialize();
        $this->setSource("XHRes_QuestionBank");
    }

    /**
     * Returns table name mapped in the model.
     *
     * @return string
     */
    public function getSource()
    {
        return 'XHRes_QuestionBank';
    }

    /**
     * Allows to query a set of records that match the specified conditions
     *
     * @param mixed $parameters
     * @return XhresQuestionbank[]
     */
    public static function find($parameters = null)
    {
        return parent::find($parameters);
    }

    /**
     * Allows to query the first record that match the specified conditions
     *
     * @param mixed $parameters
     * @return XhresQuestionbank
     */
    public static function findFirst($parameters = null)
    {
        return parent::findFirst($parameters);
    }

    /**
     * 数据预处理
     *
     * @return bool
     *
     * create by ck 20170524
     */
    public function afterFetch()
    {
        //日志处理
        $this->mQBVersionDate and $this->mQBVersionDate = json_decode(base64_decode($this->mQBVersionDate), true);
        return true;
    }

    /**
     * 保存前处理数据
     *
     * @return bool
     *
     * create by ck 20170524
     */
    public function beforeSave()
    {
        //日志处理
        $this->mQBVersionDate and $this->mQBVersionDate = base64_encode(json_encode($this->mQBVersionDate, JSON_UNESCAPED_UNICODE));
        return true;
    }

    /**
     * 根据传入id，删除对应记录并返回删除结果
     *
     * @param $ids
     * @return mixed
     *
     * create by ck 20160923
     */
    public static function deleteRecordById($ids){
        return self::find(array(
            'conditions' => "iQBankId IN ({$ids}) AND bDelete = 0"
        ))->delete();
    }

    /**
     * 根据数据载入到modal中返回
     *
     * @param array $data
     * @param bool $new
     * @return XhresQuestionbank
     *
     * create by ck 20160608
     * modify by ck 20160923    化简
     */
    public static function createRecord($data = array(), &$new)
    {
        $obj = self::findFirst($data['iQBankId']);
        if($obj){
            $new = false;
        }
        else{
            $obj = new self;
            $new = true;
        }
        $obj->assign($data);
        $obj->bDelete = 0;
        return $obj;
    }

    /**
     * @param $question_bank_id
     * @return bool
     * 根据题库ID检查是否已存在该题库ID
     */
//    public static function checkQBankId($question_bank_id){
//        $obj = self::findFirst("iQBankId = '{$question_bank_id}' AND bDelete = 0");
//        return $obj?false:true;
//    }

}
